<template>
  <div class="songs-container">
    <div class="tab-bar">
      <span class="item" :class="{active:tag=='0'}" @click="tag=0">全部</span>
      <span class="item" :class="{active:tag=='7'}" @click="tag=7">华语</span>
      <span class="item" :class="{active:tag=='96'}" @click="tag=96">欧美</span>
      <span class="item" :class="{active:tag=='8'}" @click="tag=8">日本</span>
      <span class="item" :class="{active:tag=='16'}" @click="tag=16">韩国</span>
    </div>
    <!-- 底部的table -->
    <table class="el-table playlit-table">
      <thead>
        <th></th>
        <th></th>
        <th>音乐标题</th>
        <th>歌手</th>
        <th>专辑</th>
        <th>时长</th>
      </thead>
      <tbody>
        <tr class="el-table__row" v-for="(item,index) in list" :key="index">
          <td>{{index+1}}</td>
          <td>
            <div class="img-wrap">
              <img :src="item.album.picUrl" alt="" />
              <span class="iconfont icon-play" @click="playSong(item.id)"></span>
            </div>
          </td>
          <td>
            <div class="song-wrap">
              
              <div class="name-wrap">
                <span>{{item.name}}</span>
                <span class="iconfont icon-mv" v-show="item.mvid!==0" @click="toMv(item.mvid)"></span>
              </div>
              <span v-if="item.alias.length!==0">{{item.alias[0]}}</span>
            </div>
            
          </td>
          <td>{{item.album.artists[0].name}}</td>
          <td>{{item.album.name}}</td>
          <td>{{item.duration+' '+' '}}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
  import axios from 'axios'
  // 导入时间格式化插件
  import moment from 'moment'
export default {
  name: 'songs',
  data() {
    return {
      list:[],  //存放获取的全部歌曲
      tag:'0'  //歌曲分类标签
    }
  },
  watch:{
    tag(){
      this.newSongs()
    }
  },
  created() {
    this.newSongs()
  },
  methods:{
    newSongs(){
      //获取最新音乐
      axios({
        url:'https://autumnfish.cn/top/song',
        method:'get',
        params:{
          type:this.tag
        }
      }).then(res=>{
        console.log('歌曲列表',res)
        this.list=res.data.data.slice(0,49)
        // 遍历获得的数据,对歌曲时长进行格式化
        for(var i=0;i<this.list.length;i++){
          let time = moment.duration(this.list[i].duration)
          let seconds = time._data.seconds>9?time._data.seconds:'0'+time._data.seconds
          this.list[i].duration = `${time._data.minutes}:${seconds}`
        }
      })
    },
    // 播放歌曲
    playSong(id){
      axios({
        url:'https://autumnfish.cn/song/url',
        method:'get',
        params:{
          id:id
        }
      }).then(res=>{
        console.log(res)
        this.$parent.musicUrl=res.data.data[0].url
      })
    },
    // 播放mv
    toMv(id){
      this.$router.push(`/mv?q=${id}`)
    },
  }
};
</script>

<style >

</style>
